package com.china08.yxyapi.api.manage.tec.office.stuattendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.stuattendance.impl.YxyStuAttendanceServiceImpl;
import com.china08.yxyapi.util.DateUtils;

/**
 * 查询学生考勤记录
 * 
 */
@CrossOrigin
@RestController
public class YxyStuAttendanceRecordSrhController extends YxyApiBaseController {

    @Autowired
    public YxyStuAttendanceServiceImpl yxyStuAttendanceServiceImpl;

    @RequestMapping(value = "/api_yw/manage/tec/office/stuattendance/record/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) {
        if (model.month == null || model.month.equals("")) {
            model.month = DateUtils.year(new Date()) + "-" + (DateUtils.month(new Date()) + 1);
        }
        List<Object> returnMap = yxyStuAttendanceServiceImpl.getStuAttendance(
                getProfile(request, response).getSchoolId(), model.classId,
                Integer.parseInt(model.month.substring(0, 4) + "" + model.month.substring(5)), model.signClass,
                model.keyWord, model.page, model.size);
        Long count = yxyStuAttendanceServiceImpl.getStuAttendanceCount(getProfile(request, response).getSchoolId(),
                model.classId, Integer.parseInt(model.month.substring(0, 4) + "" + model.month.substring(5)),
                model.signClass, model.keyWord, model.page, model.size);
        Pageable pageable = model.getPageable(Direction.DESC, "dayDate");
        List<RespModel> respModels = new ArrayList<YxyStuAttendanceRecordSrhController.RespModel>();
        for (Object object : returnMap) {
            @SuppressWarnings("unchecked")
            Map<String, Object> map = (Map<String, Object>) object;
            RespModel respModel = new RespModel();
            respModel.index = map.get("index").toString();
            respModel.id = map.get("id").toString();
            respModel.date = map.get("date").toString();
            respModel.stuName = map.get("stuName").toString();
            respModel.stuNo = map.get("stuNo") == null ? null : map.get("stuNo").toString();
            respModel.className = map.get("className") == null ? null : map.get("className").toString();
            respModel.signInDate = map.get("signInDate") == null ? null : map.get("signInDate").toString();
            respModel.signOutDate = map.get("signOutDate") == null ? null : map.get("signOutDate").toString();
            respModel.inspection = map.get("inspection") == null ? null : map.get("inspection").toString();
            respModel.inspectionDate = map.get("inspectionDate") == null ? null : map.get("inspectionDate").toString();
            respModel.signClass = map.get("signClass") == null ? null : map.get("signClass").toString();
            respModel.signClassDate = map.get("signClassDate") == null ? null : map.get("signClassDate").toString();
            respModels.add(respModel);
        }
        Page<RespModel> resPage = new PageImpl<YxyStuAttendanceRecordSrhController.RespModel>(respModels, pageable,
                count);
        return resPage;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiPageReqModel {
        // 班级id
        public String classId;
        // 月份
        public String month;
        // 到班状态
        public String signClass;
        // 输入参数
        public String keyWord;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // id
        public String id;
        // 编号
        public String index;
        // 日期
        public String date;
        // 学生姓名
        public String stuName;
        // 学号
        public String stuNo;
        // 班级
        public String className;
        // 到校时间
        public String signInDate;
        // 离校时间
        public String signOutDate;
        // 晨检状态
        public String inspection;
        // 晨检时间
        public String inspectionDate;
        // 到班状态
        public String signClass;
        // 到班时间
        public String signClassDate;
    }
}
